<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Generator" content="Kate, the KDE Advanced Text Editor" />
<title>tutorial6.py</title>
</head>
<body>
<pre style='color:#141312;background-color:#ffffff;'>
<i><span style='color:#008000;'>#!/usr/bin/env python</span></i>

<i><span style='color:#008000;'>&quot;&quot;&quot;********************************************************************************</span></i>
<i><span style='color:#008000;'>                             tutorial6.py</span></i>
<i><span style='color:#008000;'>                 DAE Tools: pyDAE module, www.daetools.com</span></i>
<i><span style='color:#008000;'>                 Copyright (C) Dragan Nikolic, 2010</span></i>
<i><span style='color:#008000;'>***********************************************************************************</span></i>
<i><span style='color:#008000;'>DAE Tools is free software; you can redistribute it and/or modify it under the </span></i>
<i><span style='color:#008000;'>terms of the GNU General Public License as published by the Free Software </span></i>
<i><span style='color:#008000;'>Foundation; either version 3 of the License, or (at your option) any later version.</span></i>
<i><span style='color:#008000;'>The DAE Tools is distributed in the hope that it will be useful, but WITHOUT ANY </span></i>
<i><span style='color:#008000;'>WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A </span></i>
<i><span style='color:#008000;'>PARTICULAR PURPOSE. See the GNU General Public License for more details.</span></i>
<i><span style='color:#008000;'>You should have received a copy of the GNU General Public License along with the</span></i>
<i><span style='color:#008000;'>DAE Tools software; if not, write to the Free Software Foundation, Inc., </span></i>
<i><span style='color:#008000;'>59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.</span></i>
<i><span style='color:#008000;'>********************************************************************************&quot;&quot;&quot;</span></i>

<i><span style='color:#008000;'>&quot;&quot;&quot;</span></i>
<i><span style='color:#008000;'>This is the simple port demo.</span></i>
<i><span style='color:#008000;'>Here we introduce:</span></i>
<i><span style='color:#008000;'> - Ports</span></i>
<i><span style='color:#008000;'> - Port connections</span></i>
<i><span style='color:#008000;'> - Units (instances of other models)</span></i>
<i><span style='color:#008000;'> </span></i>
<i><span style='color:#008000;'>A simple port type 'portSimple' is defined which contains only one variable 't'.</span></i>
<i><span style='color:#008000;'>Two models 'modPortIn' and 'modPortOut' are defined, each having one port of type 'portSimple'.</span></i>
<i><span style='color:#008000;'>The wrapper model 'modTutorial' instantiate these two models as its units and connects them</span></i>
<i><span style='color:#008000;'>by connecting their ports.</span></i>
<i><span style='color:#008000;'>&quot;&quot;&quot;</span></i>

<span style='color:#0000ff;'>import</span> sys
<span style='color:#0000ff;'>from</span> daetools.pyDAE <span style='color:#0000ff;'>import</span> <span style='color:#0000ff;'>*</span>
<span style='color:#0000ff;'>from</span> time <span style='color:#0000ff;'>import</span> localtime, strftime

typeNone         <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;None&quot;</span>,         <span style='color:#bf0303;'>&quot;-&quot;</span>,      <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1E10</span>,   <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1e-5</span>)
typeTemperature  <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Temperature&quot;</span>,  <span style='color:#bf0303;'>&quot;K&quot;</span>,    <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1000</span>, <span style='color:#c000c0;'>300</span>, <span style='color:#c000c0;'>1e-5</span>)
typeConductivity <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Conductivity&quot;</span>, <span style='color:#bf0303;'>&quot;W/mK&quot;</span>,   <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1E10</span>, <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1e-5</span>)
typeDensity      <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;Density&quot;</span>,      <span style='color:#bf0303;'>&quot;kg/m3&quot;</span>,  <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1E10</span>, <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1e-5</span>)
typeHeatCapacity <span style='color:#0000ff;'>=</span> daeVariableType(<span style='color:#bf0303;'>&quot;HeatCapacity&quot;</span>, <span style='color:#bf0303;'>&quot;J/KgK&quot;</span>,  <span style='color:#c000c0;'>0</span>, <span style='color:#c000c0;'>1E10</span>, <span style='color:#c000c0;'>100</span>, <span style='color:#c000c0;'>1e-5</span>)

<i><span style='color:#008000;'># Ports, like models, consist of domains, parameters and variables. Parameters and variables</span></i>
<i><span style='color:#008000;'># can be distributed as well. Here we define a very simple port, with only one variable.</span></i>
<i><span style='color:#008000;'># The process of defining ports is analogous to defining models. Domains, parameters and</span></i>
<i><span style='color:#008000;'># variables are declared in the constructor __init__ and their constructor accepts ports as</span></i>
<i><span style='color:#008000;'># the 'Parent' argument. </span></i>
<b>class</b> portSimple(daePort):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, PortType, Model, Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;&quot;</span>):
        daePort.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, PortType, Model, Description)
        
        <span style='color:#0000ff;'>self</span>.t <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;t&quot;</span>, typeNone, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;Time elapsed in the process, s&quot;</span>)

<i><span style='color:#008000;'># Here we define two models, 'modPortIn' and 'modPortOut' each having one port of type portSimple.</span></i>
<i><span style='color:#008000;'># The model 'modPortIn' contains inlet port Pin while the model 'modPortOut' contains outlet port Pout.</span></i>
<b>class</b> modPortIn(daeModel):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>None</span>, Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;&quot;</span>):
        daeModel.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent, Description)

        <span style='color:#0000ff;'>self</span>.Pin <span style='color:#0000ff;'>=</span> portSimple(<span style='color:#bf0303;'>&quot;P_in&quot;</span>, eInletPort, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;The simple port&quot;</span>)
    
    <b>def</b> DeclareEquations(<span style='color:#0000ff;'>self</span>):
        <b>pass</b>

<b>class</b> modPortOut(daeModel):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>None</span>, Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;&quot;</span>):
        daeModel.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent, Description)

        <span style='color:#0000ff;'>self</span>.Pout <span style='color:#0000ff;'>=</span> portSimple(<span style='color:#bf0303;'>&quot;P_out&quot;</span>, eOutletPort, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;The simple port&quot;</span>)
        <span style='color:#0000ff;'>self</span>.time <span style='color:#0000ff;'>=</span> daeVariable(<span style='color:#bf0303;'>&quot;Time&quot;</span>, typeNone, <span style='color:#0000ff;'>self</span>, <span style='color:#bf0303;'>&quot;Time elapsed in the process, s&quot;</span>)
    
    <b>def</b> DeclareEquations(<span style='color:#0000ff;'>self</span>):
        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;time&quot;</span>, <span style='color:#bf0303;'>&quot;Differential equation to calculate the time elapsed in the process.&quot;</span>)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.time.dt() <span style='color:#0000ff;'>-</span> <span style='color:#c000c0;'>1.0</span>

        eq <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.CreateEquation(<span style='color:#bf0303;'>&quot;Port_t&quot;</span>, <span style='color:#bf0303;'>&quot;&quot;</span>)
        eq.Residual <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>self</span>.Pout.t() <span style='color:#0000ff;'>-</span> <span style='color:#0000ff;'>self</span>.time()

<i><span style='color:#008000;'># Model 'modTutorial' declares two units mpin of type 'modPortIn' and 'mpout' of type 'modPortOut'.</span></i>
<b>class</b> modTutorial(daeModel):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>None</span>, Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;&quot;</span>):
        daeModel.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>, Name, Parent, Description)

        <span style='color:#0000ff;'>self</span>.mpin  <span style='color:#0000ff;'>=</span> modPortIn(<span style='color:#bf0303;'>&quot;Port_In&quot;</span>, <span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.mpout <span style='color:#0000ff;'>=</span> modPortOut(<span style='color:#bf0303;'>&quot;Port_Out&quot;</span>, <span style='color:#0000ff;'>self</span>)

    <b>def</b> DeclareEquations(<span style='color:#0000ff;'>self</span>):
        <i><span style='color:#008000;'># Ports can be connected by using the function ConnectPorts from daeModel class. Apparently, </span></i>
        <i><span style='color:#008000;'># ports dont have to be of the same type but must contain the same number of parameters and variables.</span></i>
        <span style='color:#0000ff;'>self</span>.ConnectPorts(<span style='color:#0000ff;'>self</span>.mpout.Pout, <span style='color:#0000ff;'>self</span>.mpin.Pin)

<b>class</b> simTutorial(daeDynamicSimulation):
    <b>def</b> <b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>):
        daeDynamicSimulation.<b><span style='color:#000e52;'>__init__</span></b>(<span style='color:#0000ff;'>self</span>)
        <span style='color:#0000ff;'>self</span>.m <span style='color:#0000ff;'>=</span> modTutorial(<span style='color:#bf0303;'>&quot;Tutorial_6&quot;</span>)
        <span style='color:#0000ff;'>self</span>.m.Description <span style='color:#0000ff;'>=</span> <span style='color:#bf0303;'>&quot;This tutorial explains how to define and connect ports. </span><span style='color:#ff80e0;'>\n</span><span style='color:#bf0303;'>&quot;</span> <span style='color:#0000ff;'>\</span>
                             <span style='color:#bf0303;'>&quot;A simple port type 'portSimple' is defined which contains only one variable 't'. &quot;</span> <span style='color:#0000ff;'>\</span>
                             <span style='color:#bf0303;'>&quot;Two models 'modPortIn' and 'modPortOut' are defined, each having one port of type 'portSimple'. &quot;</span> <span style='color:#0000ff;'>\</span>
                             <span style='color:#bf0303;'>&quot;The wrapper model 'modTutorial' instantiate these two models as its units and connects them &quot;</span> <span style='color:#0000ff;'>\</span>
                             <span style='color:#bf0303;'>&quot;by connecting their ports. &quot;</span>
                             
    <b>def</b> SetUpParametersAndDomains(<span style='color:#0000ff;'>self</span>):
        <b>pass</b>
    
    <b>def</b> SetUpVariables(<span style='color:#0000ff;'>self</span>):
        <span style='color:#0000ff;'>self</span>.m.mpout.time.SetInitialCondition(<span style='color:#c000c0;'>0</span>)
    
<i><span style='color:#008000;'># Use daeSimulator class</span></i>
<b>def</b> guiRun():
    <span style='color:#0000ff;'>from</span> PyQt4 <span style='color:#0000ff;'>import</span> QtCore, QtGui
    app <span style='color:#0000ff;'>=</span> QtGui.QApplication(sys.argv)
    simulation <span style='color:#0000ff;'>=</span> simTutorial()
    simulation.m.SetReportingOn(<span style='color:#0000ff;'>True</span>)
    simulation.ReportingInterval <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>10</span>
    simulation.TimeHorizon       <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>1000</span>
    simulator  <span style='color:#0000ff;'>=</span> daeSimulator(app, simulation)
    simulator.show()
    app.exec_()

<i><span style='color:#008000;'># Setup everything manually and run in a console</span></i>
<b>def</b> consoleRun():
    <i><span style='color:#008000;'># Create Log, Solver, DataReporter and Simulation object</span></i>
    log          <span style='color:#0000ff;'>=</span> daePythonStdOutLog()
    solver       <span style='color:#0000ff;'>=</span> daeIDASolver()
    datareporter <span style='color:#0000ff;'>=</span> daeTCPIPDataReporter()
    simulation   <span style='color:#0000ff;'>=</span> simTutorial()

    <i><span style='color:#008000;'># Enable reporting of all variables</span></i>
    simulation.m.SetReportingOn(<span style='color:#0000ff;'>True</span>)

    <i><span style='color:#008000;'># Set the time horizon and the reporting interval</span></i>
    simulation.ReportingInterval <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>10</span>
    simulation.TimeHorizon <span style='color:#0000ff;'>=</span> <span style='color:#c000c0;'>100</span>

    <i><span style='color:#008000;'># Connect data reporter</span></i>
    simName <span style='color:#0000ff;'>=</span> simulation.m.Name <span style='color:#0000ff;'>+</span> strftime(<span style='color:#bf0303;'>&quot; [</span><span style='color:#0000ff;'>%d</span><span style='color:#bf0303;'>.%m.%Y %H:%M:%S]&quot;</span>, localtime())
    <b>if</b>(datareporter.Connect(<span style='color:#bf0303;'>&quot;&quot;</span>, simName) <span style='color:#0000ff;'>==</span> <span style='color:#0000ff;'>False</span>):
        sys.exit()

    <i><span style='color:#008000;'># Initialize the simulation</span></i>
    simulation.Initialize(solver, datareporter, log)
    
    <i><span style='color:#008000;'># Save the model report and the runtime model report </span></i>
    simulation.m.SaveModelReport(simulation.m.Name <span style='color:#0000ff;'>+</span> <span style='color:#bf0303;'>&quot;.xml&quot;</span>)
    simulation.m.SaveRuntimeModelReport(simulation.m.Name <span style='color:#0000ff;'>+</span> <span style='color:#bf0303;'>&quot;-rt.xml&quot;</span>)

    <i><span style='color:#008000;'># Solve at time=0 (initialization)</span></i>
    simulation.SolveInitial()

    <i><span style='color:#008000;'># Run</span></i>
    simulation.Run()

<b>if</b> <b><span style='color:#000000;'>__name__</span></b> <span style='color:#0000ff;'>==</span> <span style='color:#bf0303;'>&quot;__main__&quot;</span>:
    runInGUI <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>True</span>
    <b>if</b> <b><span style='color:#000000;'>len</span></b>(sys.argv) <span style='color:#0000ff;'>&gt;</span> <span style='color:#c000c0;'>1</span>:
        <b>if</b>(sys.argv[<span style='color:#c000c0;'>1</span>] <span style='color:#0000ff;'>==</span> <span style='color:#bf0303;'>'console'</span>):
            runInGUI <span style='color:#0000ff;'>=</span> <span style='color:#0000ff;'>False</span>
    <b>if</b> runInGUI:
        guiRun()
    <b>else</b>:
        consoleRun()
</pre>
</body>
</html>
